/<template>
  <div class='login'>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          type="password"
          v-model="ruleForm.username"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>   
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from '../utils/axios1'
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      }else {
        callback();
      }
    };
    
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ validator: validatePass, trigger: "blur" }],
        password: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");
        axios.get('/login',{
          params:{
            username:this.ruleForm.username,
            password:this.ruleForm.password
          }
        }).then(res=>{
          if(res.data.code==200){
            alert('登录成功');
            window.localStorage.setItem('token',this.ruleForm.username);
            this.$router.push('/home/classify')
          }else{
            alert('登录失败')
          }
        })
        } else {
          // console.log("error submit!!");    
          return false;
        }
      });
    },
  },
};
</script>

<style>
.login{
  width: 400px;
  height: 300px;
  margin: 0 auto;
}
</style>